package com.data.modules.wrt.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.data.common.constant.WebsocketConst;
import com.data.modules.message.websocket.WebSocket;
import com.data.modules.wrt.entity.WrtAlarmLog;
import com.data.modules.wrt.mapper.WrtAlarmLogMapper;
import com.data.modules.wrt.service.IWrtAlarmLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Description: 慧锐通报警日志
 * @Author: data-boot
 * @Date:   2023-09-04
 * @Version: V1.0
 */
@Service
public class WrtAlarmLogServiceImpl extends ServiceImpl<WrtAlarmLogMapper, WrtAlarmLog> implements IWrtAlarmLogService {

    @Autowired
    private WrtAlarmLogMapper mapper;

    @Resource
    private WebSocket webSocket;

    @Override
    public Map queryAlarmLog() {
        return mapper.searchAlarmLog();
    }

    @Override
    public void addAlarmInfo(WrtAlarmLog po,String user) {

        if(ObjectUtil.isEmpty(po.getDealTime())){

            JSONObject obj = new JSONObject();
            obj.put(WebsocketConst.MSG_TXT, po.getAlarmName()+":"+po.getDeviceName());
            obj.put(WebsocketConst.MSG_TYPE,po.getAlarmName().equals("紧急报警")?1:3);
            if(ObjectUtil.isNotEmpty(user)){
                webSocket.sendMessage(user,obj.toJSONString());
            }else{//所有人
                webSocket.sendMessage(obj.toJSONString());
            }

            this.mapper.insert(po);
        }else {
            List<WrtAlarmLog> list = mapper.queryBySnAndTime(po);
            if(ObjectUtil.isEmpty(list)){
                this.mapper.insert(po);
            }else {
                WrtAlarmLog log = list.get(0);
                log.setDealTime(po.getDealTime());
                log.setDealDesc(po.getDealDesc());
                this.mapper.updateById(log);
            }
        }
    }
}
